/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pooproj.repositorio;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import pooproj.basica.Cliente;
import pooproj.erro.ConexaoException;
import pooproj.erro.ConsultaException;
import pooproj.util.GerenciadorConexao;
import pooproj.util.GerenciadorConexaoImpl;

/**
 *
 * @author Daniella
 */
public class RepositorioClienteImpl implements RepositorioCliente {

    //obter a instância utilizada para qualquer método desta classe
    private GerenciadorConexao gc = GerenciadorConexaoImpl.getInstancia();
    
    @Override
    public void inserir(Cliente c) throws ConsultaException, ConexaoException {
        Connection con = gc.conectar();
        String sql = "INSERT INTO cliente (nomeCliente) VALUES (?)";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setString(1, c.getNomeCliente());
            pstm.executeUpdate();//usado para INSERT,UPDATE e DELETE
        } catch (SQLException e) {
            throw new ConsultaException(e.getMessage());
        } finally {
            gc.desconectar(con);
        
      }
    
    }
    @Override
    public void alterar(Cliente c) throws ConsultaException, ConexaoException {
        Connection con = gc.conectar();
        String sql = "UPDATE cliente SET nomeCliente =? WHERE codCliente=?";
        
        try{
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setString(1, c.getNomeCliente());
            pstm.setInt(2, c.getCodCliente());
            pstm.executeUpdate();//usado para INSERT,UPDATE e DELETE
        } catch (SQLException e){
            throw new ConsultaException(e.getMessage());
        } finally {
            gc.desconectar(con);
        }
    }
    
    @Override
    public void excluir(Cliente c) throws ConsultaException, ConexaoException {
        Connection con = gc.conectar();
        String sql = "DELETE FROM cliente WHERE codCliente=?";
        try{
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setInt(1, c.getCodCliente());
            pstm.executeUpdate();//usado para INSERT,UPDATE e DELETE
      } catch (SQLException e){
          throw new ConsultaException(e.getMessage());
      } finally {
            gc.desconectar(con);
        }
    }

    @Override
    public ArrayList<Cliente> listar() throws ConsultaException, ConexaoException {
        ArrayList<Cliente> lista = new ArrayList();
        Cliente c;
        Connection con = gc.conectar();
        String sql = "SELECT codCliente, nomeCliente FROM cliente";
        
        try{
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sql);//usado para SELECT
            while(rs.next()){
                c = new Cliente();
                c.setCodCliente(rs.getInt("codCliente"));
                c.setNomeCliente(rs.getString("nomeCliente"));
                lista.add(c);
            }
            stm.close();
            rs.close();
        } catch (SQLException e){
            throw new ConsultaException(e.getMessage());
        }
         return lista;
    }

    @Override
    public Cliente consultar(Integer id) throws ConsultaException, ConexaoException {
        Cliente c = null;
        Connection con =  gc.conectar();
        String sql = "SELECT codCliente, nomeCliente FROM cliente WHERE codCliente=?";
        
        try{
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setInt(1, id);
            ResultSet rs = pstm.executeQuery();//usado para SELECT
            if(rs.next()){
                c = new Cliente();
                c.setCodCliente(rs.getInt("codCliente"));
                c.setNomeCliente(rs.getString("nomeCliente"));
            }
        } catch (SQLException e){
            throw new ConsultaException(e.getMessage());
        } finally {
            gc.desconectar(con);
        }
        return c;
    }

    @Override
    public Cliente consultar(String nome) throws ConsultaException, ConexaoException {
        Cliente c = null;
        Connection con =  gc.conectar();
        String sql = "SELECT codCliente, nomeCliente FROM cliente WHERE nomeCliente=?";
        
        try{
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setString(1, nome);
            ResultSet rs = pstm.executeQuery();//usado para SELECT
            if(rs.next()){
                c = new Cliente();
                c.setCodCliente(rs.getInt("codCliente"));
                c.setNomeCliente(rs.getString("nomeCliente"));
            }
        } catch (SQLException e){
            throw new ConsultaException(e.getMessage());
        } finally {
            gc.desconectar(con);
        }
        return c;
    }
    
}
